package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProblemBackListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProblemBackOriginal;
import com.isunland.managesystem.entity.ProblemBackQueryContent;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/11/17.
 */
public class ProblemBackListFragment extends BaseListFragment {

  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ProblemBackListFragment.EXTRA_VALUE";
  private final int DETAIL = 0;
  private final int QUERY = 1;
  private ProblemBackListAdapter mAdapter;
  private String regDateName;
  private String regDateCode;
  private String subPerson;
  private String backContent;
  private String dataStatusName;
  private String dataStatusCode;
  private String ifReplyName;
  private String ifReplyCode;
  private ProblemBackQueryContent mqueryContent;
  private CurrentUser mUser;
  private int mPage = 1;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.back_list);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mUser = CurrentUser.newInstance(getActivity());
    regDateCode = "";
    regDateName = "全部";
    subPerson = mUser.getRealName();
    backContent = "";
    dataStatusCode = "";
    dataStatusName = "全部";
    ifReplyCode = "";
    ifReplyName = "全部";
    mqueryContent = new ProblemBackQueryContent(regDateName, regDateCode, subPerson, backContent,
        dataStatusName, dataStatusCode, ifReplyName, ifReplyCode);

    super.onCreate(savedInstanceState);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_search, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), ProblemBackActivity.class);
        startActivityForResult(intent, DETAIL);
        return true;
      case R.id.menu_item_querys:
        Intent query = new Intent(getActivity(), ProblemBackQueryActivity.class);
        query.putExtra(ProblemBackQueryFragment.EXTRA_CONTENT, mqueryContent);
        startActivityForResult(query, QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  /**
   * 获取url
   */
  @Override public String getUrl() {
    return ApiConst.URL_PROBLEM_BACK_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("page", mPage + "");
    params.put("pageSize", "20");
    params.put("feedback", backContent);
    params.put("regStaffName", subPerson);
    params.put("regDate", regDateCode);
    params.put("ifReply", ifReplyCode);
    params.put("dataStatus", dataStatusCode);
    params.put("orderField", "f.reg_date");
    params.put("orderSeq", "desc");
    return params;
  }

  /**
   * 解析响应
   */
  @Override public void resolveResponse(String json) {
    ProblemBackOriginal original = new Gson().fromJson(json, ProblemBackOriginal.class);
    ArrayList<ProblemBackOriginal.ProblemBackContent> rows =
        (ArrayList<ProblemBackOriginal.ProblemBackContent>) original.getRows();
    setAdapter(rows);
  }

  private void setAdapter(ArrayList<ProblemBackOriginal.ProblemBackContent> rows) {
    if (rows == null) {
      rows = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new ProblemBackListAdapter(getActivity(), rows);
      setListAdapter(mAdapter);
      return;
    }
    if (mPage == 1) {
      mAdapter.clear();
    }
    mAdapter.addAll(rows);
    ((ProblemBackListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ProblemBackOriginal.ProblemBackContent content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), ProblemBackDetailActivity.class);
    i.putExtra(EXTRA_VALUE, content);
    startActivityForResult(i, DETAIL);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {

    PullToRefreshBase.Mode mode = refreshView.getCurrentMode();
    if (mode.equals(PullToRefreshBase.Mode.PULL_FROM_START)) {

      mPage = 1;
    }
    if (mode.equals(PullToRefreshBase.Mode.PULL_FROM_END)) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL) {
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      mPage = 1;
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
    if (requestCode == QUERY) {
      mqueryContent = (ProblemBackQueryContent) data.getSerializableExtra(
          ProblemBackQueryFragment.EXTRA_CONTENT);
      regDateCode = mqueryContent.getRegDateCode();
      subPerson = mqueryContent.getSubPerson();
      backContent = mqueryContent.getBackContent();
      ifReplyCode = mqueryContent.getIfReplyCode();
      dataStatusCode = mqueryContent.getDataStatusCode();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      mPage = 1;
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
  }
}
